package com.time.company.components.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.time.company.R;
import com.time.company.app.AddressManager;
import com.time.company.base.BaseActivity;
import com.time.company.servermodel.BasicBean;
import com.time.company.servermodel.User;
import com.time.company.servermodel.login.SubmitCompanyPara;
import com.time.company.utils.JpushUtils;
import com.time.company.utils.NetWorkUtils;
import com.time.company.utils.ToastUtils;
import com.time.company.webserver.helper.WebServerManager;
import com.time.company.webserver.request.BasicRequest;
import com.time.company.webserver.request.requestsParamters.LoginParameters;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

/**
 * Created by Bing on 2017/11/2.
 * E-mail is tx16bing@163.com
 */

public class CompanyInfoActivity extends BaseActivity {

    private EditText etName, etCreditCode, etContact, etTelephone, etEmail;
    private Button btnNext;

    private static final String EXTRA_COMPANY_PARA = "extra_company_para";

    private Request request;
    private static final int USER_INFO_WHAT = 27;

    public static void start(Activity activity, int requestCode, SubmitCompanyPara para) {
        Intent intent = new Intent(activity, CompanyInfoActivity.class);
        intent.putExtra(EXTRA_COMPANY_PARA, para);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_info, "");
        assignViews();
    }

    private void assignViews() {
        etName = findView(R.id.et_company_name);
        etCreditCode = findView(R.id.et_company_credit_code);
        etContact = findView(R.id.et_company_contact);
        etTelephone = findView(R.id.et_company_telephone);
        etEmail = findView(R.id.et_company_email);
        btnNext = findView(R.id.btn_company_next);
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkForm()) {
                    return;
                }
                SubmitCompanyPara para = (SubmitCompanyPara) getIntent().getSerializableExtra(EXTRA_COMPANY_PARA);
                registerCompany(para);
            }
        });
    }

    /**
     * 注册公司
     *
     * @param para
     */
    private void registerCompany(SubmitCompanyPara para) {
        if (NetWorkUtils.isNetAvailable(this)) {
            request = new BasicRequest(AddressManager.getInstance().getAddress());
            LoginParameters parameters = new LoginParameters();
            parameters.setCommand(getString(R.string.command_registerCompanyManual));
            parameters.setUserId(application.getAccount());
            parameters.setName(etName.getText().toString());
            parameters.setCreditCode(etCreditCode.getText().toString());
            parameters.setLogoUrl(para.getLogoUrl());
            parameters.setLicenseUrl(para.getLicenseUrl());
            parameters.setMainBusiness(para.getMainBusiness());
            parameters.setCompanyNature(para.getCompanyNature());
            parameters.setScale(para.getScale());
            parameters.setAddress(para.getAddress());
            parameters.setIntroduction("0");
            parameters.setContactName(etContact.getText().toString());
            parameters.setContactTelephone(etTelephone.getText().toString());
            parameters.setContactEmail(etEmail.getText().toString());
            if (TextUtils.isEmpty(JpushUtils.getRegisterId(this))) {
                parameters.setJpushId(getString(R.string.zero));
            } else {
                parameters.setJpushId(JpushUtils.getRegisterId(this));
            }
            request.add(getString(R.string.command), parameters.getCommand());
            request.add(getString(R.string.platform), parameters.getPlatform());
            request.add(getString(R.string.data), new Gson().toJson(parameters));
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(this, request, this, 20,
                    true, true, true);
        } else {
            ToastUtils.showShort(getString(R.string.network_error));
        }
    }

    private boolean checkForm() {
        boolean isFormValid = true;

        if (TextUtils.isEmpty(etName.getText().toString())) {
            ToastUtils.showShort("请输入企业名称");
            isFormValid = false;
        } else if (TextUtils.isEmpty(etCreditCode.getText().toString())) {
            ToastUtils.showShort("请输入统一社会信用代码");
            isFormValid = false;
        } else if (TextUtils.isEmpty(etContact.getText().toString())) {
            ToastUtils.showShort("请输入联系人");
            isFormValid = false;
        } else if (TextUtils.isEmpty(etTelephone.getText().toString())) {
            ToastUtils.showShort("请输入电话号码");
            isFormValid = false;
        } else if (TextUtils.isEmpty(etEmail.getText().toString())) {
            ToastUtils.showShort("请输入邮箱");
            isFormValid = false;
        }

        return isFormValid;
    }

    // 加载个人信息
    private void getUserInfo() {
        if (NetWorkUtils.isNetAvailable(this)) {
            request = new BasicRequest(AddressManager.getInstance().getAddress());
            LoginParameters parameters = new LoginParameters();
            parameters.setCommand(getString(R.string.command_getUserInfo));
            parameters.setUserId(application.getAccount());
            if (TextUtils.isEmpty(JpushUtils.getRegisterId(this))) {
                parameters.setJpushId(getString(R.string.zero));
            } else {
                parameters.setJpushId(JpushUtils.getRegisterId(this));
            }
            request.add(getString(R.string.command), parameters.getCommand());
            request.add(getString(R.string.platform), parameters.getPlatform());
            request.add(getString(R.string.data), new Gson().toJson(parameters));
            request.setCancelSign(toString());
            WebServerManager.getInstance().add(this, request, this, USER_INFO_WHAT,
                    true, true, true);
        } else {
            ToastUtils.showShort(getString(R.string.network_error));
        }
    }

    @Override
    public void onSucceed(int what, Response<BasicBean> response) {
        BasicBean basicBean = response.get();

        if (basicBean == null) return;

        if (basicBean.isSuccess()) {
            ToastUtils.showShort(basicBean.getMsg());
            getUserInfo();
        } else if (what == USER_INFO_WHAT) {
            User user = basicBean.parseData(User.class);
            mVMcreator.setUserViewModel(user);
            CommitSCActivity.start(this, user.getAccountName(), user.getPassword(), "0");
            setResult(RESULT_OK);
            finish();
        }
    }

}
